<?php

/**
 * generate_lorem()
 * 
 * This function is useful for generating lorem ipsum text for development data
 * 
 * NOTE: I did not create this function, and I am not taking credit for it.  
 * 
 * @param mixed $word_count
 * @param bool $title
 * @return
 */
function generate_lorem($word_count, $title = false)
{
	$dictionary = array("abbas", "abdo", "abico", "abigo", "abluo", "accumsan", "acsi", "ad", "adipiscing", "aliquam", "aliquip", "amet", "antehabeo", "appellatio", "aptent", "at", "augue", "autem", "bene", "blandit", "brevitas", "caecus", "camur", "capto", "causa", "cogo", "comis", "commodo", "commoveo", "consectetuer", "consequat", "conventio", "cui", "damnum", "decet", "defui", "diam", "dignissim", "distineo", "dolor", "dolore", "dolus", "duis", "ea", "eligo", "elit", "enim", "erat", "eros",
		"esca", "esse", "et", "eu", "euismod", "eum", "ex", "exerci", "exputo", "facilisi", "facilisis", "fere", "feugiat", "gemino", "genitus", "gilvus", "gravis", "haero", "hendrerit", "hos", "huic", "humo", "iaceo", "ibidem", "ideo", "ille", "illum", "immitto", "importunus", "imputo", "in", "incassum", "inhibeo", "interdico", "iriure", "iusto", "iustum", "jugis", "jumentum", "jus", "laoreet", "lenis", "letalis", "lobortis", "loquor", "lucidus", "luctus", "ludus", "luptatum", "macto", "magna",
		"mauris", "melior", "metuo", "meus", "minim", "modo", "molior", "mos", "natu", "neo", "neque", "nibh", "nimis", "nisl", "nobis", "nostrud", "nulla", "nunc", "nutus", "obruo", "occuro", "odio", "olim", "oppeto", "os", "pagus", "pala", "paratus", "patria", "paulatim", "pecus", "persto", "pertineo", "plaga", "pneum", "populus", "praemitto", "praesent", "premo", "probo", "proprius", "quadrum", "quae", "qui", "quia", "quibus", "quidem", "quidne", "quis", "ratis", "refero", "refoveo", "roto",
		"rusticus", "saepius", "sagaciter", "saluto", "scisco", "secundum", "sed", "si", "similis", "singularis", "sino", "sit", "sudo", "suscipere", "suscipit", "tamen", "tation", "te", "tego", "tincidunt", "torqueo", "tum", "turpis", "typicus", "ulciscor", "ullamcorper", "usitas", "ut", "utinam", "utrum", "uxor", "valde", "valetudo", "validus", "vel", "velit", "veniam", "venio", "vereor", "vero", "verto", "vicis", "vindico", "virtus", "voco", "volutpat", "vulpes", "vulputate", "wisi", "ymo", "zelus");
	$dictionary_flipped = array_flip($dictionary);

	$greeking = '';

	if (!$title)
	{
		$words_remaining = $word_count;
		while ($words_remaining > 0)
		{
			$sentence_length = mt_rand(3, 10);
			$words = array_rand($dictionary_flipped, $sentence_length);
			$sentence = implode(' ', $words);
			$greeking .= ucfirst($sentence) . '. ';
			$words_remaining -= $sentence_length;
		}
	}
	else
	{
		// Use slightly different method for titles.
		$words = array_rand($dictionary_flipped, $word_count);
		$greeking = ucwords(implode(' ', $words));
	}

	// Work around possible php garbage collection bug. Without an unset(), this
	// function gets very expensive over many calls (php 5.2.11).
	unset($dictionary, $dictionary_flipped);
	return trim($greeking);
}

/**
 * developer_data_create()
 * 
 * generates a bunch of dev data for the developer table
 * 
 * @return void
 */
function developer_data_create()
{
	$fp = fopen('./includes/development/InitDeveloper.sql', 'r');
	$sql = fread($fp, filesize('./includes/development/InitDeveloper.sql'));
	$sql = explode("\r\n", $sql);
	
	foreach ($sql as $query)
	{
		db_query($query);
	}
}

/**
 * feature_data_create()
 * 
 * generates a bunch of dev data for the feature table
 * 
 * @return void
 */
function feature_data_create()
{
	for ($i = 0; $i < 100; $i++)
	{
		$description = generate_lorem(3, FALSE);
		$details = generate_lorem(12, FALSE);
		
		db_query("INSERT INTO feature (description, details) VALUES ('%s', '%s')", array($description, $details));
	}
}

/**
 * milestone_data_create()
 * 
 * generates a bunch of dev data for the milestone table
 * 
 * @return void
 */
function milestone_data_create()
{
	for ($i = 0; $i < 10; $i++)
	{
		$name = generate_lorem(3, FALSE);
		$date = time() + mt_rand(0, 7776000);
		
		db_query("INSERT INTO milestone (name, date) VALUES ('%s', %d)", array($name, $date));
	}
}

/**
 * task_data_create()
 * 
 * generates a bunch of dev data for the tasks table
 * 
 * This function must be called after the other 3 because it is dependent on data existing in the other tables
 * 
 * @return void
 */
function task_data_create()
{
	$result = db_query('SELECT did FROM developer WHERE did > 0');
	$developers = array();
	while ($did = db_result($result))
	{
		$developers[] = $did;
	}
	
	$result = db_query('SELECT fid FROM feature WHERE fid > 0');
	$features = array();
	while ($fid = db_result($result))
	{
		$features[] = $fid;
	}
	
	$result = db_query('SELECT mid FROM milestone WHERE mid > 0');
	$milestones = array();
	while ($mid = db_result($result))
	{
		$milestones[] = $mid;
	}
	
	$count_dev = count($developers) - 1;
	$count_feat = count($features) - 1;
	$count_mile = count($milestones) - 1;
	
	$status_list = array('P', 'I', 'C');
	for ($i = 0; $i < 250; $i++)
	{
		$description = generate_lorem(3, FALSE);
		$details = generate_lorem(15, FALSE);
		$did = $developers[mt_rand(0, $count_dev)];
		$fid = $features[mt_rand(0, $count_feat)];
		$mid = $milestones[mt_rand(0, $count_mile)];
		$eet = mt_rand(0, 518400);
		$status = $status_list[array_rand($status_list)];
		
		db_query("INSERT INTO task (description, details, did, fid, mid, eet, status) VALUES ('%s', '%s', %d, %d, %d, %d,'%s')", array($description, $details, $did, $fid, $mid, $eet, $status));
	}
}
